package com.jxwifi.cloud.quickclean.wxapi;

import android.content.Intent;
import android.os.Bundle;

import com.jxwifi.cloud.quickclean.app.ClientBasicActivity;
import com.jxwifi.cloud.quickclean.app.StateUtils;
import com.jxwifi.cloud.quickclean.bean.RespBaseBean;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.simple.eventbus.EventBus;

/** 微信登录 回调页面
 * date：2018年10月25日
 * time：16点24分
 * company：广东快捷帮信息技术有限公司
 */
public class WXEntryActivity extends ClientBasicActivity implements IWXAPIEventHandler {
    private IWXAPI mIWXAPI;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mIWXAPI = WXAPIFactory.createWXAPI(this, StateUtils.WX_APPID, false);
        mIWXAPI.registerApp(StateUtils.WX_APPID);
        mIWXAPI.handleIntent(getIntent(), this);
        openEventBus();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        mIWXAPI.handleIntent(intent, this);
    }

    // 微信发送请求到第三方应用时，会回调到该方法
    @Override
    public void onReq(BaseReq req) {

    }

    // 第三方应用发送到微信的请求处理后的响应结果，会回调到该方法
    @Override
    public void onResp(BaseResp baseResp) {
            int type = baseResp.getType();
            if(type == ConstantsAPI.COMMAND_SENDAUTH){ //授权登录回调
                type =1;
            } else if(type == ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX){ //微信分享
                type = 0;
            }else if (baseResp.getType() == ConstantsAPI.COMMAND_LAUNCH_WX_MINIPROGRAM){
                WXLaunchMiniProgram.Resp launchMiniProResp = (WXLaunchMiniProgram.Resp) baseResp;
                String extraData =launchMiniProResp.extMsg; //对应小程序组件 <button open-type="launchApp"> 中的 app-parameter 属性
            }
            RespBaseBean mBean=new RespBaseBean();
                  mBean.setTyoe(type);
                  mBean.setBaseResp(baseResp);
            switch (baseResp.errCode){
                case BaseResp.ErrCode.ERR_OK:  //成功
                    //mThirdPart.success(type, baseResp);
                    EventBus.getDefault().post(mBean, "e-wx-success");
                    break;
                case BaseResp.ErrCode.ERR_USER_CANCEL: //失败
                    EventBus.getDefault().post(mBean, "e-wx-cancel");
                    //mThirdPart.Cancel(type);
                    break;
                default:
                    EventBus.getDefault().post(mBean, "e-wx-error");
                    //mThirdPart.Error(type, baseResp.errCode, baseResp.errStr);
                    break;
            }
        finish();
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, 0);
    }


}